﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Collections;
using System.ComponentModel;

namespace ScarfMaker
{
    /// <summary>
    /// MaterialMenu.xaml 的交互逻辑
    /// </summary>
    public partial class MaterialMenu : UserControl
    {
        private Models.vm_MaterialMenu meterialMenu = null;
        public Hashtable hsMeterialMenuFiles = new Hashtable();
        public MaterialMenu(Models.vm_MaterialMenu mm)
        {
            InitializeComponent();
            meterialMenu = mm;
            this.DataContext = mm;
            wpMenuType.Children.Clear();
            foreach(var mt in mm.listMeterialType)
            {
                RadioButton rb = new RadioButton();
                rb.Cursor = Cursors.Hand;
                rb.Content = mt.TypeName;
                rb.GroupName = mm.MenuName;
                rb.Width = 100;
                rb.Tag = Guid.NewGuid().ToString();
                rb.Checked += rb_Checked;
                hsMeterialMenuFiles.Add(rb.Tag, mt.listFileInfo);

                wpMenuType.Children.Add(rb);
            }
        }

        void rb_Checked(object sender, RoutedEventArgs e)
        {
            wpMeterial.Children.Clear();
            string guid = ((RadioButton)sender).Tag as string;
            List<FileInfo> listFileInfo =(List<FileInfo>) hsMeterialMenuFiles[guid];
            if(listFileInfo!=null)
            {
                foreach(FileInfo fi in listFileInfo)
                {
                    Image img = new Image();
                    img.Source = new BitmapImage(new Uri("file://"+fi.FullName));
                    img.Width = 70;
                    img.Tag = fi.FullName;
                    img.Cursor = Cursors.Hand;
                    img.MouseLeftButtonDown += img_MouseLeftButtonDown;
                    wpMeterial.Children.Add(img);
                }
            }
        }

        void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (SelectingImage != null)
            {
                SelectingImage(sender, e);
            }
        }
        /// <summary>
        /// 选择图片时的事件，sender为image类型，其tag存储文件的FullName。
        /// </summary>
        public event EventHandler SelectingImage;

    }
}
